package com.qd.qx.ui.module1;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.universaladapter.ViewHolderHelper;
import com.aspsine.irecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import com.qd.common.base.BaseActivity;
import com.qd.common.commonutils.ImageLoaderUtils;
import com.qd.common.commonutils.ToastUitl;
import com.qd.qx.R;
import com.qd.qx.bean.MembersBean;
import com.qd.qx.bean.PeopleBean;
import com.qd.qx.ui.module2.TongXunLuSelectActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Code is far away from bug with the animal protecting
 * Author：YS on 2018/10/31
 * Function：群所有成员展示
 */
public class GroupMembersShowActivity extends BaseActivity {
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvMore)
    TextView tvMore;
    @BindView(R.id.ivMore)
    ImageView ivMore;
    @BindView(R.id.ircv)
    IRecyclerView ircv;

    CommonRecycleViewAdapter adapter;
    List<MembersBean.DataBean> list = new ArrayList<>();
    String jid;

    /**
     * 入口
     */
    public static void startAction(Activity activity, String jid, List<MembersBean.DataBean> list) {
        Intent intent = new Intent(activity, GroupMembersShowActivity.class);
        intent.putExtra("jid", jid);
        intent.putExtra("list", (Parcelable) list);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                com.qd.common.R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_groupmembersshow;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        ivMore.setVisibility(View.GONE);
        jid = getIntent().getStringExtra("jid");
        list = (List<MembersBean.DataBean>) getIntent().getSerializableExtra("list");
        initAdapter();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if ((intent.getFlags() | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) > 0) {
            if (android.os.Build.VERSION.SDK_INT >= 19 && !isTaskRoot()) {
                ActivityManager tasksManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                tasksManager.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_NO_USER_ACTION);
            }
        }
    }
    private void initAdapter() {
        adapter = new CommonRecycleViewAdapter<MembersBean.DataBean>(this, R.layout.chatmore_itemlayout) {
            @Override
            public void convert(ViewHolderHelper helper, final MembersBean.DataBean bean) {
                ImageView imageView = helper.getView(R.id.ivImg);
                TextView tvName = helper.getView(R.id.tvName);
                switch (bean.getName()) {
                    case "add":
                        tvName.setText("");
                        imageView.setImageResource(R.drawable.kuang_jia);
                        break;
                    case "minus":
                        tvName.setText("");
                        imageView.setImageResource(R.drawable.kuang_jian);
                        break;
                    default:
                        tvName.setText(bean.getName() + "");
                        ImageLoaderUtils.displaySmallPhoto(GroupMembersShowActivity.this, imageView, bean.getHeadImg());
                        break;
                }
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (bean.getName()) {
                            case "add":
                                TongXunLuSelectActivity.startAction(GroupMembersShowActivity.this, "0", "通讯录", jid, "", "");
                                break;
                            case "minus":
                                ChatRemoveActivity.startAction(GroupMembersShowActivity.this, jid);
                                break;
                            default:
                                break;
                        }
                    }
                });
            }

            @Override
            public void onBindViewHolder(ViewHolderHelper holder, final int position) {
                super.onBindViewHolder(holder, position);
                holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUitl.showShort(position + "");
                    }
                });
            }
        };
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 5);
        ircv.setLayoutManager(gridLayoutManager);
        adapter.addAll(list);
        ircv.setAdapter(adapter);
    }


    @OnClick({R.id.ivBack})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
